//
//  DeviceTableViewCell.m
//  JunZeWristband
//
//  Created by Kendama on 2017/8/25.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "DeviceTableViewCell.h"

NSString *const kDeviceTableViewCellIdentifier = @"kDeviceTableViewCellIdentifier";

@implementation DeviceTableViewCell

+ (instancetype)dequeueReusableWithTableView:(UITableView *)tableView
{
    DeviceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDeviceTableViewCellIdentifier];
    if (cell == nil)
    {
        cell = [[DeviceTableViewCell alloc]init];
    }
    return cell;
}

- (instancetype)init
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDeviceTableViewCellIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self masLayoutSubview];
        
        
    }
    return self;
}

- (void)didClickConnectButton
{

    if (self.connectDeviceBlcok)
    {
        self.connectDeviceBlcok(self.connectButton.selected);
    }
    
    if (self.connectButton.selected)
    {
        self.connectButton.selected = NO;
    }
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.contentView;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(54));
        make.right.mas_equalTo(self.connectButton.mas_left);
    }];
    
    [self.connectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.right.mas_equalTo(superView.mas_right).with.offset(px1080Width(-54));
        make.width.mas_equalTo(px1080Width(250));
    }];
}

- (UILabel *)nameLabel
{
    if(!_nameLabel)
    {
        UIView *superView = self.contentView;
        _nameLabel = [[UILabel alloc]init];
        [superView addSubview:_nameLabel];
        [_nameLabel setFont:[UIFont systemFontOfSize:15]];
        [_nameLabel setTextColor:[UIColor darkGrayColor]];
    }
    return _nameLabel;
}


- (UIButton *)connectButton
{
    if(!_connectButton)
    {
        UIView *superView = self.contentView;
        _connectButton = [[UIButton alloc]init];
        [superView addSubview:_connectButton];
        [_connectButton setTitle:MyNSLocalizedString(@"连接") forState:UIControlStateNormal];
        [_connectButton setTitle:MyNSLocalizedString(@"断开") forState:UIControlStateSelected];
        [_connectButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_connectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_connectButton setBackgroundImage:[UIImage imageNamed:@"unbind"] forState:UIControlStateNormal];
        [_connectButton setBackgroundImage:[UIImage imageNamed:@"bind"] forState:UIControlStateSelected];
        [_connectButton addTarget:self action:@selector(didClickConnectButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _connectButton;
}
@end
